const axios = require('axios');

const types = {
    isInvisibleWidget: true,
    type: "MY_QDATA",
    icon: "https://waddle.coco-central.cn/static/img/logo/logo-white.svg",
    title: "Qdata云数据表",
    version: "1.0.0",
    isGlobalWidget: true,
    properties: [],
    methods: [],
    events: [],
};

class Widget extends InvisibleWidget {
    constructor(props) {
        super(props);

    }

}

types['methods'].push({
    key: 'set',
    label: '设置',
    params: [
      {
          key: 'table_id',
          label: '数据表ID',
          valueType: 'string',
          defaultValue: "",
      },
      {
          key: 'table_key',
          label: '数据表秘钥',
          valueType: 'string',
          defaultValue: "",
      },
      {
          key: 'name',
          label: '键名',
          valueType: 'string',
          defaultValue: "",
      },
      {
          key: 'value',
          label: '值',
          valueType: 'string',
          defaultValue: "",
      },],


})
Widget.prototype.set = function (table_id,table_key,name,value,) {

  axios.post('https://qdata.space/api.php',{
      data: {'table_id': table_id, 'mode': 'set', 'name': name, 'value': value, 'key': (CryptoJS.SHA256(([new Date().getMinutes(),table_id,table_key].join(''))))},

    })
    .then((response) => {
      this.widgetLog((response.data));

    })
    .catch((error) => {
      this.widgetLog((response.data));

    });

}
types['methods'].push({
    key: 'read',
    label: '获取',
    params: [
      {
          key: 'table_id',
          label: '数据表ID',
          valueType: 'string',
          defaultValue: "",
      },
      {
          key: 'table_key',
          label: '数据表秘钥',
          valueType: 'string',
          defaultValue: "",
      },
      {
          key: 'name',
          label: '键名',
          valueType: 'string',
          defaultValue: "",
      },],


})
Widget.prototype.read = function (table_id,table_key,name,) {

  axios.post('https://qdata.space/api.php',{
      data: {'table_id': table_id, 'mode': 'read', 'name': name, 'value': value, 'key': (CryptoJS.SHA256(([new Date().getMinutes(),table_id,table_key].join(''))).toString())},

    })
    .then((response) => {
      this.widgetLog((response.data));

    })
    .catch((error) => {
      this.widgetLog((response.data));

    });

}
exports.types = types;
exports.widget = Widget;
